spring_challenge_r1_23_vku

 avatar
unknown
javascript
2 years ago
1.9 kB
47
Indexable
class ChallengeClass {
    constructor() {
        this.adj = []
        this.visit = []
        this.mark = []
        for (let i = 0; i <= 100; i++) {
            this.adj.push([])
            this.visit.push(false)
            this.mark.push(false)
        }
        this.count = 0
    }
    dfs(u) {
        if (this.visit[u]) return 0
        this.visit[u] = true
        this.count += 1
        // console.log(" at " + u + ", have " + this.count)
        for (const v of this.adj[u])
            this.dfs(v)
    }
    Calculate(str) {
        let items = str.split(/\s/)
        let cur = -1
        for (const it of items) {
            let num = 0, f = 0
            for (const ds of it) {
                let d = parseInt(ds)
                if (isNaN(d)) break;
                f += 1
                num *= 10; num += d
            }
            if (!f) continue
            this.mark[num] = true
            if (cur == -1) cur = num;
            else {
                // console.log(num + ' - ' + cur)
                this.adj[cur].push(num)
                this.adj[num].push(cur)
                if (it.indexOf(']') != -1) cur = -1;
            }
        }
        let cnt = 0, big = 0
        for (let i = 0; i < 100; i++) {
            if (this.mark[i]) {
                if (this.visit[i]) continue
                cnt += 1
                this.count = 0
                this.dfs(i)
                big = Math.max(big, this.count)
            }
        }
        // console.log(big + ' and ' + cnt)
        return cnt * big
    }
}

// ans = 
// new ChallengeClass().Calculate('0 ->[ 5, 6, 7], 1 ->[ 12, 14], 2 ->[ 3, 8], 3 ->[ 2, 4, 8, 10], 4 ->[ 3, 5], 5 ->[ 0, 4], 6 ->[ 0, 7], 7 ->[ 0, 6], 8 ->[ 2, 3], 9 ->[ 16], 10 ->[ 3, 11], 11 ->[ 10], 12 ->[ 1, 13], 13 ->[ 12], 14 ->[ 1, 15], 15 ->[ 14], 16 ->[ 9]')
// console.log(ans)
Editor is loading...