292B - Network Topology - CodeForces Solution


graphs implementation *1200

Please click on ads to support us..

Python Code:

while True:
    try:
        x = input()
        n, m = list(map(int, x.split()))
        freqs = {(i+1): 0 for i in range(n)}
        for i in range(m):
            x = input()
            a, b = list(map(int, x.split()))
            freqs[a] += 1
            freqs[b] += 1
                freq = {}
        for key, val in freqs.items():
            if val not in freq:
                freq[val] = 1
            else:
                freq[val] += 1
        
        if len(freq) == 2:
            if 1 in freq.keys():
                if freq[1] == 2:
                    print("bus topology")
                elif freq[1] == n - 1:
                    print("star topology")
                else:
                    print("unknown topology")
            else:
                print("unknown topology")
        elif len(freq) == 1:
            if 2 in freq.keys():
                print("ring topology")
            elif 1 in freq.keys():
                print("bus topology")
            else:
                print("unknown topology")
        else:
            print("unknown topology")

    except EOFError:
        break
   	 				 	    	 	    				

C++ Code:

#include "bits/stdc++.h"
using namespace std;
#define int               long long
#define pb                push_back
#define ppb               pop_back
#define pf                push_front
#define ppf               pop_front
#define all(x)            (x).begin(),(x).end()
#define uniq(v)           (v).erase(unique(all(v)),(v).end())
#define sz(x)             (int)((x).size())
#define fr                first
#define sc                second
#define pii               pair<int,int>
#define rep(i,a,b)        for(int i=a;i<b;i++)
#define mem1(a)           memset(a,-1,sizeof(a))
#define mem0(a)           memset(a,0,sizeof(a))
#define ppc               __builtin_popcount
#define ppcll             __builtin_popcountll

template<typename T1, typename T2>istream& operator>>(istream& in, pair<T1, T2> &a) {in >> a.fr >> a.sc; return in;}
template<typename T1, typename T2>ostream& operator<<(ostream& out, pair<T1, T2> a) {out << a.fr << " " << a.sc; return out;}
template<typename T, typename T1>T amax(T &a, T1 b) {if (b > a)a = b; return a;}
template<typename T, typename T1>T amin(T &a, T1 b) {if (b < a)a = b; return a;}

const long long INF = 1e18;
const int32_t M = 1e9 + 7;
const int32_t MM = 998244353;

const int N = 1e6 + 7;

const int MOD = 1e9 + 7;
const long long MOD2 = static_cast<long long>(MOD) * MOD;


/* ----------------------------------------------------------------------------------------------------------*/






void solve() {


	int n, m, u, v;
	cin >> n >> m;

	int degree[n + 1];

	for (int i = 1; i <= n; i++) {
		degree[i] = 0;

	}

	for (int i = 1; i <= m; i++) {
		cin >> u >> v;
		degree[u]++;
		degree[v]++;
	}


	int cnt1 = 0, cnt2 = 0, cntN = 0;

	for (int i = 1; i <= n; i++) {

		if (degree[i] == 1) {
			cnt1++;
		}

		if (degree[i] == 2) {
			cnt2++;
		}

		if (degree[i] == (n - 1)) {
			cntN++;
		}
	}



	if (cnt2 == n) {
		cout << "ring topology";
		return;
	}


	if (cnt2 == (n - 2) && cnt1 == 2) {
		cout << "bus topology";
		return;
	}




	if (cntN == 1 && cnt1 == (n - 1)) {
		cout << "star topology";
		return;
	}

	cout << "unknown topology";



}


signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
#ifdef SIEVE
	sieve();
#endif
#ifdef NCR
	init();
#endif




	int t = 1;
	// cin >> t;
	while (t--) solve();
	return 0;
}



Comments

Submit
0 Comments
More Questions

1607A - Linear Keyboard
EQUALCOIN Equal Coins
XOREQN Xor Equation
MAKEPAL Weird Palindrome Making
HILLSEQ Hill Sequence
MAXBRIDGE Maximise the bridges
WLDRPL Wildcard Replacement
1221. Split a String in Balanced Strings
1002. Find Common Characters
1602A - Two Subsequences
1555A - PizzaForces
1607B - Odd Grasshopper
1084A - The Fair Nut and Elevator
1440B - Sum of Medians
1032A - Kitchen Utensils
1501B - Napoleon Cake
1584B - Coloring Rectangles
1562B - Scenes From a Memory
1521A - Nastia and Nearly Good Numbers
208. Implement Trie
1605B - Reverse Sort
1607C - Minimum Extraction
1604B - XOR Specia-LIS-t
1606B - Update Files
1598B - Groups
1602B - Divine Array
1594B - Special Numbers
1614A - Divan and a Store
2085. Count Common Words With One Occurrence
2089. Find Target Indices After Sorting Array